home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Utilities / PGP / source / pgp50i-b8a / tools / mempool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-04  |  3.9 KB  |  180 lines

  1. /*
  2.  * mempool.c - Pooled memory allocation, similar to GNU obstacks.
  3.  *
  4.  * Copyright (c) 1993, 1994  Colin Plumb.  All rights reserved.
  5.  * For licensing and other legal details, see the file legal.c.
  6.  */
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>    /* For malloc() & free() */
  11.  
  12. #include "mempool.h"
  13.  
  14. /*
  15.  * The memory pool allocation functions
  16.  *
  17.  * These are based on a linked list of memory blocks, usually of uniform
  18.  * size.  New memory is allocated from the tail of the current block,
  19.  * until that is inadequate, then a new block is allocated.
  20.  * The entire pool can be freed at once by calling memPoolFree().
  21.  */
  22.  
  23. struct PoolBuf {
  24.     struct PoolBuf *next;
  25.     unsigned size;
  26.     /* Data follows */
  27. };
  28.  
  29. /* A global pool for miscellaneous stuff. */
  30. struct MemPool MiscPool = { 0, 0, 0, 1024, 0, 0, 0};
  31.  
  32.  
  33. /* Page size to use */
  34. #ifdef MSDOS
  35. #define ALLOC_UNIT 1024
  36. #else
  37. #define ALLOC_UNIT 4096
  38. #endif
  39.  
  40. static struct MemPool EmptyPool = { 0, 0, 0, ALLOC_UNIT, 0 , 0, 0};
  41.  
  42. /*
  43.  * Initialize the pool for first use
  44.  */
  45. void
  46. memPoolInit(struct MemPool *pool)
  47. {
  48.     *pool = EmptyPool;
  49. }
  50.  
  51. /* Set the pool's purge function */
  52. void
  53. memPoolSetPurge(struct MemPool *pool, int (*purge)(void *), void *arg)
  54. {
  55.     pool->purge = purge;
  56.     pool->purgearg = arg;
  57. }
  58.  
  59. /*
  60.  * Free all the memory in the pool
  61.  */
  62. void
  63. memPoolEmpty(struct MemPool *pool)
  64. {
  65.     struct PoolBuf *buf;
  66.  
  67.     while ((buf = pool->head) != 0) {
  68.         pool->head = buf->next;
  69.         free(buf);
  70.     }
  71.     pool->freespace = 0;
  72.     pool->totalsize = 0;
  73. }
  74.  
  75.  
  76. /*
  77.  * Restore a pool to a marked position, freeing subsequently allocated
  78.  * memory.
  79.  */
  80. void
  81. memPoolCutBack(struct MemPool *pool, struct MemPool const *cutback)
  82. {
  83.     struct PoolBuf *buf;
  84.  
  85.     assert(pool);
  86.     assert(cutback);
  87.  
  88.     assert(pool->totalsize >= cutback->totalsize);
  89.  
  90.     while((buf = pool->head) != cutback->head) {
  91.         pool->head = buf->next;
  92.         free(buf);
  93.     }
  94.     *pool = *cutback;
  95. }
  96.  
  97.  
  98. /*
  99.  * Structure and string allocations.
  100.  * To make alingment work, strings and other objects
  101.  * are allocated from different pools.
  102.  */
  103.  
  104. /*
  105.  * Allocate a chunk of memory for a structure.  Alignment is assumed to be
  106.  * a power of 2.  It could be generalized, if that ever becomes relevant.
  107.  * Note that alignment is to the beginning of an allocated chunk, which
  108.  * is guaranteed by ANSI to be as aligned as can possibly matter.
  109.  */
  110. void *
  111. memPoolAlloc(struct MemPool *pool, unsigned len, unsigned alignment)
  112. {
  113.     char *p;
  114.     unsigned t;
  115.  
  116.     /* Where to allocate next object */
  117.     p = pool->freeptr;
  118.     /* How far it is from the beginning of the chunk. */
  119.     t = p - (char *)pool->head;
  120.     /* How much to round up freeptr to make alignment */
  121.     t = -t & --alignment;
  122.  
  123.     /* Okay, does it fit? */
  124.     if (pool->freespace >= len+t) {
  125.         pool->freespace -= len+t;
  126.         p += t;
  127.         pool->freeptr = p + len;
  128.         return p;
  129.     }
  130.  
  131.     /* It does not fit in the current chunk.  Go for a bigger chunk. */
  132.  
  133.     /* First, figure out how much to skip at the beginning of the chunk */
  134.     alignment &= -(unsigned)sizeof(struct PoolBuf);
  135.     alignment += sizeof(struct PoolBuf);
  136.     /* Then, figure out a chunk size that will fit */
  137.     t = pool->chunksize;
  138.     assert(t);
  139.     while (len + alignment > t)
  140.         t *= 2;
  141.     while ((p = malloc(t)) == 0) {
  142.         /* If that didn't work, try purging or smaller allocations */
  143.         if (!pool->purge || !pool->purge(pool->purgearg)) {
  144.             t /= 2;
  145.             if (len + alignment > t)
  146.                 fputs("Out of memory!\n", stderr);
  147.                 exit (1);    /* Failed */
  148.         }
  149.     }
  150.  
  151.     /* Update the various pointers. */
  152.     pool->totalsize += t;
  153.     ((struct PoolBuf *)p)->next = pool->head;
  154.     ((struct PoolBuf *)p)->size = t;
  155.     pool->head = (struct PoolBuf *)p;
  156.     pool->freespace = t - len - alignment;
  157.     p += alignment;
  158.     pool->freeptr = p + len;
  159.  
  160.     return p;
  161. }
  162.  
  163. #ifdef DEADCODE
  164. /*
  165.  * Store a string in the table, returning a pointer to the string.
  166.  */
  167. char const *
  168. memPoolStore(struct MemPool *pool, char const *str)
  169. {
  170.     unsigned len = strlen(str) + 1;
  171.     char *p;
  172.  
  173.     p = memPoolAlloc(pool, len, 1);
  174.     if (p)
  175.         memcpy(p, str, len);
  176.  
  177.     return p;
  178. }
  179. #endif
  180.